package com.navcoo.todolist.service.impl;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.navcoo.todolist.mapper.TodoDataMapper;
import com.navcoo.todolist.pojo.dao.TodoDataDAO;
import com.navcoo.todolist.pojo.dto.UpdateTodoDataDTO;
import com.navcoo.todolist.service.UpdateTodoData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 更新待办数据
 *
 * @author Navcoo
 * @date 2024-03-04
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UpdateTodoDataImpl implements UpdateTodoData {

    private final TodoDataMapper todoDataMapper;

    /**
     * 更新待办数据
     *
     * @param instId 本月id
     * @param isDone 完成
     * @return {@link JSONObject}
     * @throws Exception 异常
     */
    @Override
    public JSONObject updateTodoData(String instId, Boolean isDone) throws Exception {

        // 查询数据
        QueryWrapper<TodoDataDAO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TodoDataDAO::getIsDelete, false);
        wrapper.lambda().eq(TodoDataDAO::getInstId, instId);


        // 创建更新数据
        TodoDataDAO todoDataDAO = new TodoDataDAO();
        todoDataDAO.setIsDone(isDone);
        todoDataDAO.setUpdatedAt(LocalDateTime.now());

        // 更新数据
        int update = todoDataMapper.update(todoDataDAO, wrapper);

        JSONObject result = new JSONObject();

        if (update <= 0) {
            log.info("更新数据失败");
        } else {
            // 成功
            log.info("更新数据成功");
            result.set("instId", instId);
            result.set("isDone", isDone);
        }

        return result;
    }
}
